package com.blockchain.coupon.controller;

import com.blockchain.coupon.entity.BankStaff;
import com.blockchain.coupon.service.BankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by flynn on 17-3-8.
 */
@Controller
@RequestMapping(value = "/bank")
public class BankController {

    private  String SUCCESS = "";
    @Autowired
    private BankService bankService;
    /**
     * 银行员工注册
     * 请求参数:
     *          bankStaffAccount
     *          bankStaffPassword
     *          bankStaffType
     * 响应结果: resultCode
     */
    @RequestMapping(value = "/bankStaffRegister", method = RequestMethod.POST)
    public String bankStaffRegister(@RequestParam("username") String account, @RequestParam("password") String password,
                                    @RequestParam("positionOptions") String  position , Map<String, String> map) {

        BankStaff bankStaff = new BankStaff();
        bankStaff.setBankStaffAccount(account);
        bankStaff.setBankStaffPassword(password);
        bankStaff.setBankStaffType(position);
        String resultCode = this.bankService.bankStaffRegister(bankStaff);
        if(resultCode.equals("1")){
            map.put("resultCode", "恭喜您注册成功，请登录");
        }else if(resultCode.equals("2")) {
            map.put("resultCode", "账号已存在，请重新填写");
        }else {
            map.put("resultCode", "Sorry,注册失败，请重新注册");
        }
        SUCCESS = "bank/login";
        return SUCCESS;
    }

    /**
     * 银行员工登录
     * 请求参数:
     *          bankStaffAccount
     *          bankStaffPassword
     *          bankStaffSecondPassword
     * 响应结果: resultCode
     */
    @RequestMapping(value="/bankStaffLogin", method=RequestMethod.POST)
    public String bankStaffLogin(@RequestParam("username") String account, @RequestParam("password") String password,
                                 Map<String, String> map, HttpSession session) {
        Map<String, Object> m = this.bankService.bankStaffLogin(account, password);
        String resultCode = m.get("resultCode").toString();
        BankStaff bankStaff = (BankStaff) m.get("bankStaff");
        if(resultCode.equals("2") ) {
            map.put("resultCode", "用户名错误，请重新输入");
            SUCCESS = "bank/login";
        } else if(resultCode.equals("3")) {
            map.put("resultCode","密码错误，请重新输入");
            SUCCESS = "bank/login";
        }else if(resultCode == "1"){
            String position = bankStaff.getBankStaffType();
            if(position.equals("1")) {
                SUCCESS = "redirect:../main/toFirstTrial";
            }else if(position.equals("2")) {
                SUCCESS = "redirect:../main/toReCheck";
            }
            // 将用户写入session
            session.setAttribute("currentBankStaff", bankStaff);
        }
        return SUCCESS;
    }

    // 商户通过或拒绝审核
    @RequestMapping(value = "/merchantPassOperation", method = RequestMethod.GET)
    public String merchantPass(@RequestParam("merchantId") String id,
                               @RequestParam("status") String status,
                               @RequestParam(value = "operatorOpinion", required = false) String operatorOpinion ,
                               HttpSession session) throws Exception {
        // System.out.println(status + "---" + id);
        // 1. 将此商户id的商户的stauts更新: 1--通过， 2--拒绝
        String resultCode = this.bankService.merchantPass(status,id);
        // 判断是操作员还是审核员
        String position = ((BankStaff)session.getAttribute("currenBankStaff")).getBankStaffType();
        if(position.equals("1"))
            SUCCESS = "redirect:../main/toFirstTrial.action";
        else if(position.equals("2"))
            SUCCESS = "redirect:../main/toReCheck.action";
        System.out.println(operatorOpinion);
        return SUCCESS;
    }
}
